package com.tiantian.quanminxiu.presenter;

import com.shenglian.utils.utils.CommonUtils;
import com.tiantian.quanminxiu.api.AnchorService;
import com.tiantian.quanminxiu.utils.ApplicationBase;
import com.tiantian.quanminxiu.utils.Constants;
import com.tiantian.quanminxiu.utils.UserData;
import com.tiantian.quanminxiu.view.EditInfoView;

import rx.android.schedulers.AndroidSchedulers;

/**
 * Created by admin on 2016/8/11.
 */
public class EditInfoPresent implements Presenter<EditInfoView> {

    private EditInfoView editInfoView;

    @Override
    public void attachView(EditInfoView view) {
        this.editInfoView = view;
    }

    @Override
    public void detachView() {
        editInfoView = null;
    }


    public void saveUserInfo(String NickName,String Signature,int Sex){
        editInfoView.Loading_Open();
        ApplicationBase app = ApplicationBase.get(editInfoView.getContext());
        AnchorService service = app.getAnchorService();
        service.user_userinfo_update(UserData.Current.mid, UserData.Current.sign, Constants.APP_TYPE,
                NickName,Signature,Sex)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(app.defaultSubscribeScheduler())
                .finallyDo(()->{
                    editInfoView.Loading_Close();
                })
                .subscribe(str -> {
                    UserData.Base.NickName = NickName;
                    UserData.Base.Signature = Signature;
                    UserData.Base.Sex = Sex;
                    UserData.Reload.Fragment_Mine = true;
                    editInfoView.saveUserInfo();
                }, throwable -> {
                    CommonUtils.showToask(editInfoView.getContext(), throwable.getMessage());
                });

    }

}
